﻿using System;

namespace HomeWork4
{
    //事件发送者
    class Time
    {
        //声明关于事件的委托
        public delegate void TickEventHandler(object sender, EventArgs e);
        public delegate void AlarmEventHandler(object sender, EventArgs e);

        //声明事件
        public event TickEventHandler Tick;
        public event AlarmEventHandler Alarm;

        //编写引发事件的函数
        public void OnTick()
        {
            if (this.Tick!= null)
            {
                Console.WriteLine("正在走时");
                this.Tick(this, new EventArgs());   //走时
            }
        }
        public void OnAlarm()
        {
            if (this.Alarm != null)
            {
                Console.WriteLine("响铃");
                this.Alarm(this, new EventArgs());   //发出警报
            }
        }
    }

    //事件接收者
    class Clock
    {
        //编写事件处理程序
        void HostHandleAlarm(object sender, EventArgs e)
        {
            Console.WriteLine("闹钟正在运行");
        }

        //注册事件处理程序
        public Clock(Time time)
        {
            time.Alarm += new Time.AlarmEventHandler(HostHandleAlarm);
            time.Tick+= new Time.TickEventHandler(HostHandleAlarm);
        }
        
    }

    //触发事件
    class Program
    {
        static void Main(string[] args)
        {
            Time time = new Time();
            Clock clock = new Clock(time);
            time.OnTick();
            time.OnAlarm();
            Console.ReadLine();
        }
    }
}
